package com.example.erat.controller;

import com.example.erat.dto.ResponseResult;
import com.example.erat.dto.StatisticsDTO;
import com.example.erat.service.StatisticsService;
import com.example.erat.util.ChartUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 统计控制器
 */
@RestController
@RequestMapping("/api/statistics")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    /**
     * 统计实验提交情况
     */
    @GetMapping
    public ResponseResult<StatisticsDTO> statisticsByClassAndCourse(
            @RequestParam Long classId, 
            @RequestParam Long courseId, 
            @RequestParam String folderPath) {
        return ResponseResult.success(statisticsService.statisticsByClassAndCourse(classId, courseId, folderPath));
    }

    /**
     * 导出学生维度统计结果到Excel
     */
    @GetMapping("/export/student")
    public ResponseEntity<byte[]> exportStudentStatistics(
            @RequestParam Long classId, 
            @RequestParam Long courseId, 
            @RequestParam String folderPath) {
        
        byte[] excelBytes = statisticsService.exportStudentStatistics(classId, courseId, folderPath);
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
        headers.setContentDispositionFormData("attachment", "student_statistics.xlsx");
        headers.setContentLength(excelBytes.length);
        
        return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
    }

    /**
     * 导出实验维度统计结果到Excel
     */
    @GetMapping("/export/experiment")
    public ResponseEntity<byte[]> exportExperimentStatistics(
            @RequestParam Long classId, 
            @RequestParam Long courseId, 
            @RequestParam String folderPath) {
        
        byte[] excelBytes = statisticsService.exportExperimentStatistics(classId, courseId, folderPath);
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
        headers.setContentDispositionFormData("attachment", "experiment_statistics.xlsx");
        headers.setContentLength(excelBytes.length);
        
        return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
    }
    
    /**
     * 生成实验提交率折线图
     */
    @GetMapping("/chart/submission-rate")
    public ResponseEntity<byte[]> generateSubmissionRateChart(
            @RequestParam Long classId, 
            @RequestParam Long courseId, 
            @RequestParam String folderPath) {
        
        StatisticsDTO statistics = statisticsService.statisticsByClassAndCourse(classId, courseId, folderPath);
        byte[] chartBytes = ChartUtil.getInstance().generateSubmissionRateChart(statistics.getExperimentStatistics());
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        headers.setContentDispositionFormData("attachment", "submission_rate_chart.png");
        headers.setContentLength(chartBytes.length);
        
        return new ResponseEntity<>(chartBytes, headers, HttpStatus.OK);
    }
    
    /**
     * 生成学生缺交次数柱状图
     */
    @GetMapping("/chart/missing-count")
    public ResponseEntity<byte[]> generateStudentMissingCountChart(
            @RequestParam Long classId, 
            @RequestParam Long courseId, 
            @RequestParam String folderPath,
            @RequestParam(defaultValue = "10") int maxStudents) {
        
        StatisticsDTO statistics = statisticsService.statisticsByClassAndCourse(classId, courseId, folderPath);
        byte[] chartBytes = ChartUtil.getInstance().generateStudentMissingCountChart(
                statistics.getStudentStatistics(), maxStudents);
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        headers.setContentDispositionFormData("attachment", "student_missing_count_chart.png");
        headers.setContentLength(chartBytes.length);
        
        return new ResponseEntity<>(chartBytes, headers, HttpStatus.OK);
    }
} 